#define DEBUG
#include <cstdio>

using namespace std;

const int maxn=100000, maxm=500000;

void initSet(int n, int set[]) {
  for (int i=1; i<=n; i++) {
    set[i] = i;
  }
}

int getFa(int f[], int o) {
  return f[o] = (f[o]!=o ? getFa(f, f[o]) : o);
}

void merge(int f[], int a, int b) {
  a = getFa(f, a), b = getFa(f, b);
  if (a!=b) {
    f[b] = a;
  }
}

int main() {
  freopen("graph.in", "r", stdin);
  freopen("graph.out", "w", stdout);

  static int set[maxn+1];
  static long long sides[maxm+1][3];
  static bool book[maxm+1];
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i=1; i<=m; i++) {
    scanf("%lld %lld %lld", sides[i]+1, sides[i]+2, sides[i]);
    book[i] = true;
  }

  long long ans=0;
  for (int i=63; i>=0; i--) {
    initSet(n, set);
    for (int j=1; j<=m; j++) {
      if (book[j] && (sides[j][0]>>i)&1) {
	merge(set, sides[j][1], sides[j][2]);
      }
    }
    if (getFa(set, 1)==getFa(set, n)) {
      for (int j=1; j<=m; j++) {
	if (((sides[j][0]>>i)&1)==0) {
	  book[j] = false;
	}
      }
      ans |= 1LL<<i;
    }
  }

  printf("%lld", ans);

  fclose(stdin);
  fclose(stdout);
  return 0;
}
